﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace RegisterationCommon.Common
{
    public class BaseLibrary
    {
        #region 限制文本中只能包含数字内容
        /// <summary>
        /// 限制文本中只能包含数字内容
        /// </summary>
        /// <param name="str">文本内容</param>
        /// <returns></returns>
        public static string StrNumberFilter(string str)
        {
            if (str == null || str == "")
            {
                return str;
            }
            str = str.Replace("０", "0");
            str = str.Replace("１", "1");
            str = str.Replace("２", "2");
            str = str.Replace("３", "3");
            str = str.Replace("４", "4");
            str = str.Replace("５", "5");
            str = str.Replace("６", "6");
            str = str.Replace("７", "7");
            str = str.Replace("８", "8");
            str = str.Replace("９", "9");
            return Regex.Replace(str, @"[^\d|\.]*", "");
        }
        #endregion

        #region 字符串验证
        /// <summary>
        /// 字符串验证
        /// </summary>
        /// <param name="source">文本框的值</param>
        /// <param name="enableNull">是否为空</param>
        /// <param name="eVer">验证类别</param>
        /// <param name="errMsgCode">提示信息编号</param>
        /// <returns></returns>
        public static bool Verification(string source, bool enableNull, EVerification eVer, int errMsgCode)
        {
            return Verification(source, enableNull, eVer);
        }

        /// <summary>
        /// 字符串验证
        /// </summary>
        /// <param name="source">文本框的值</param>
        /// <param name="enableNull">是否为空</param>
        /// <param name="eVer">验证类别</param>
        /// <param name="errMsgCode">提示信息编号</param>
        /// <returns></returns>
        public static bool Verification(string source, bool enableNull, EVerification eVer)
        {
            bool verPass = true;
            source = source.Trim();

            //空值验证
            if (source == "")
            {
                if (!enableNull)
                {
                    verPass = false;
                }
                else
                {
                    verPass = true;
                }
            }
            if (verPass == false) return verPass;

            //表达式验证
            switch (eVer)
            {
                //身份证验证
                case EVerification.IDcardLength:
                    if (!Regex.IsMatch(source, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase))
                    {
                        verPass = false;
                    }
                    break;
                //整数验证
                case EVerification.Integer:
                    if (!Regex.IsMatch(source, @"^\d+$", RegexOptions.IgnoreCase))
                    {
                        verPass = false;
                    }
                    break;
                //小数验证
                case EVerification.Decimal:
                    if (!Regex.IsMatch(source, @"^[0-9]+(.[0-9]{2})?$", RegexOptions.IgnoreCase))
                    {
                        verPass = false;
                    }
                    break;
                //安全字符串验证
                case EVerification.SafeString:
                    if (!Regex.IsMatch(source, @"^\w+$", RegexOptions.IgnoreCase))
                    {
                        verPass = false;
                    }
                    break;
                //EMail验证
                case EVerification.EMail:
                    if (!Regex.IsMatch(source, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", RegexOptions.IgnoreCase))
                    {
                        verPass = false;
                    }
                    break;
                //双字节字符验证
                case EVerification.Chinese:
                    if (!Regex.IsMatch(source, @"^[^\x00-\xff]+$", RegexOptions.IgnoreCase))
                    {
                        verPass = false;
                    }
                    break;
                //危险字符串验证
                case EVerification.DisBadChar:
                    if (
                        source.ToLower().Contains('\'') || source.ToLower().Contains('\"') ||
                        source.ToLower().Contains('*') || source.ToLower().Contains('%') ||
                        source.ToLower().Contains("select ") || source.ToLower().Contains("update ") ||
                        source.ToLower().Contains("delete ") || source.ToLower().Contains("insert ")
                    )
                    {
                        verPass = false;
                    }
                    break;
                default:
                    verPass = true;
                    break;
            }
            return verPass;
        }
        #endregion

        #region 字符串处理
        /// <summary>
        /// 去除非法字符
        /// </summary>
        /// <param name="str">要去除的字符串</param>
        /// <returns>返回替换后的字符串</returns>
        public static string ReplaceChar(string str)
        {
            if (str == null || str == "")
            {
                return str;
            }
            str = str.Replace("<", "");
            str = str.Replace(">", "");
            str = str.Replace("\"", "");
            str = str.Replace("\'", "");
            str = str.Trim();
            return str;
        }

        /// <summary>
        /// 去除非法与特殊字符
        /// </summary>
        /// <param name="str">要去除的字符串</param>
        /// <returns>返回替换后的字符串</returns>
        public static string ReplaceBadChar(string str)
        {
            if (str == null || str == "")
            {
                return "";
            }
            else
            {
                str = str.Trim();
                str = BaseLibrary.ReplaceChar(str);
                str = str.Replace(" ", "");
                str = str.Replace("@", "");
                str = str.Replace("#", "");
                str = str.Replace("$", "");
                str = str.Replace("%", "");
                str = str.Replace("^", "");
                str = str.Replace("&", "");
                str = str.Replace("~", "");
                str = str.Replace("[\n\r]", "");
            }
            return str;
        }

        /// <summary>
        /// 去除回车换行
        /// </summary>
        /// <param name="str">要去除的字符串</param>
        /// <returns>返回替换后的字符串</returns>
        public static string ReplaceEnter(string str)
        {
            if (str == null || str == "")
            {
                return str;
            }
            str = str.Replace("\n\r", "");
            str = str.Replace("\t", "");
            //str = str.Replace(" ", "");
            return str;
        }

        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="str">要过滤的字符串</param>
        /// <returns>返回过滤后的字符串</returns>
        public static string FilterChar(string str)
        {
            if (str == null || str == "")
            {
                return str;
            }
            str = str.Replace("<", "&#60;");
            str = str.Replace(">", "&#62;");
            str = str.Replace("\"", "&#34;");
            str = str.Replace("\'", "&#39;");
            str = str.Replace("[\n\r]", "&#8249;");
            str = str.Trim();
            return str;
        }

        /// <summary>
        /// 恢复被过滤的字符
        /// </summary>
        /// <param name="str">要恢复的字符串</param>
        /// <returns>返回恢复后的字符串</returns>
        public static string ResumeChar(string str)
        {
            if (str == null || str == "")
            {
                return str;
            }
            str = str.Replace("&#60;", "<");
            str = str.Replace("&#62;", ">");
            str = str.Replace("&#34;", "\"");
            str = str.Replace("&#39;", "\'");
            str = str.Replace("&#8249;", Environment.NewLine);//换行字符，在Windows下是\r\n，在Linux(Mono)下是\n
            return str;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">来源字符串</param>
        /// <returns></returns>
        public static string ConvertToMD5(string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = Encoding.Default.GetBytes(str);
            byte[] output = md5.ComputeHash(result);

            str = BitConverter.ToString(output).Replace("-", "");
            return str;
        }
        #endregion

        #region 金额转换为大写
        /// <summary>
        ///金额转换成大写
        /// </summary>
        /// <param name="LowerMoney">要转换的金额</param>
        /// <returns>大写金额</returns>
        public static string MoneyToChinese(string LowerMoney)
        {
            string functionReturnValue = null;
            bool IsNegative = false;//是否是负数
            LowerMoney = LowerMoney.Trim();
            if (LowerMoney.Trim().Substring(0, 1) == "-")
            {
                LowerMoney = LowerMoney.Trim().Remove(0, 1);
                IsNegative = true;
            }
            try
            {
                string strLower = null;
                string strUPart = null;
                string strUpper = null;
                int iTemp = 0;
                LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();
                //保留到两位小数
                if (LowerMoney.IndexOf(".") > 0)
                {
                    if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2)
                    {
                        LowerMoney = LowerMoney + "0";
                    }
                }
                else
                {
                    LowerMoney = LowerMoney + ".00";
                }
                strLower = LowerMoney;

                iTemp = 1;
                strUpper = "";
                while (iTemp <= strLower.Length)
                {
                    switch (strLower.Substring(strLower.Length - iTemp, 1))
                    {
                        case ".":
                            strUPart = "圆";
                            break;
                        case "0":
                            strUPart = "零";
                            break;
                        case "1":
                            strUPart = "壹";
                            break;
                        case "2":
                            strUPart = "贰";
                            break;
                        case "3":
                            strUPart = "叁";
                            break;
                        case "4":
                            strUPart = "肆";
                            break;
                        case "5":
                            strUPart = "伍";
                            break;
                        case "6":
                            strUPart = "陆";
                            break;
                        case "7":
                            strUPart = "柒";
                            break;
                        case "8":
                            strUPart = "捌";
                            break;
                        case "9":
                            strUPart = "玖";
                            break;
                    }
                    switch (iTemp)
                    {
                        case 1:
                            strUPart = strUPart + "分";
                            break;
                        case 2:
                            strUPart = strUPart + "角";
                            break;
                        case 3:
                            strUPart = strUPart + "";
                            break;
                        case 4:
                            strUPart = strUPart + "";
                            break;
                        case 5:
                            strUPart = strUPart + "拾";
                            break;
                        case 6:
                            strUPart = strUPart + "佰";
                            break;
                        case 7:
                            strUPart = strUPart + "仟";
                            break;
                        case 8:
                            strUPart = strUPart + "万";
                            break;
                        case 9:
                            strUPart = strUPart + "拾";
                            break;
                        case 10:
                            strUPart = strUPart + "佰";
                            break;
                        case 11:
                            strUPart = strUPart + "仟";
                            break;
                        case 12:
                            strUPart = strUPart + "亿";
                            break;
                        case 13:
                            strUPart = strUPart + "拾";
                            break;
                        case 14:
                            strUPart = strUPart + "佰";
                            break;
                        case 15:
                            strUPart = strUPart + "仟";
                            break;
                        case 16:
                            strUPart = strUPart + "万";
                            break;
                        default:
                            strUPart = strUPart + "";
                            break;
                    }
                    strUpper = strUPart + strUpper;
                    iTemp = iTemp + 1;
                }

                strUpper = strUpper.Replace("零拾", "零");
                strUpper = strUpper.Replace("零佰", "零");
                strUpper = strUpper.Replace("零仟", "零");
                strUpper = strUpper.Replace("零零零", "零");
                strUpper = strUpper.Replace("零零", "零");
                strUpper = strUpper.Replace("零角零分", "整");
                strUpper = strUpper.Replace("零分", "");
                strUpper = strUpper.Replace("零角", "零");
                strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
                strUpper = strUpper.Replace("亿零万零圆", "亿圆");
                strUpper = strUpper.Replace("零亿零万", "亿");
                strUpper = strUpper.Replace("零万零圆", "万圆");
                strUpper = strUpper.Replace("万零圆", "万圆");
                strUpper = strUpper.Replace("零亿", "亿");
                strUpper = strUpper.Replace("零万", "万");
                strUpper = strUpper.Replace("零圆", "圆");
                strUpper = strUpper.Replace("零零", "零");
                //对壹圆以下的金额的处理
                if (strUpper.Substring(0, 1) == "圆")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "零")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "角")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "分")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "整")
                {
                    strUpper = "零圆整";
                }
                functionReturnValue = strUpper;
            }
            catch
            {
                functionReturnValue = "零圆整";
            }
            if (IsNegative == true)
            {
                return "负" + functionReturnValue;
            }
            else
            {
                return functionReturnValue;
            }
        }
        #endregion

        #region 获取随机码
        /// <summary>
        /// 获取随机码
        /// </summary>
        /// <param name="num">位数</param>
        /// <returns>返回验证码</returns>
        public static string GetRandomCode(int num, ERandomCodeType type)
        {
            string str = "";
            switch (type)
            {
                case ERandomCodeType.Both:
                    str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    break;
                case ERandomCodeType.English:
                    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    break;
                case ERandomCodeType.Numeral:
                    str = "0123456789";
                    break;
            }
            string randomCode = "";
            Random rd = new Random();
            for (int i = 0; i < num; i++)
            {
                randomCode += str.Substring(rd.Next(0, str.Length), 1);
            }
            return randomCode;
        }
        #endregion

        #region 将汉字转换成字母并只取首字母

        /// <summary>
        /// 将汉字转换成字母并只取首字母
        /// </summary>
        /// <param name="ChineseStr"></param>
        /// <returns></returns>
        public static string ChineseCap(string ChineseStr)
        {
            string Capstr = "";
            byte[] ZW = new byte[2];
            long ChineseStr_int;
            string CharStr, ChinaStr = "";
            for (int i = 0; i <= ChineseStr.Length - 1; i++)
            {
                CharStr = ChineseStr.Substring(i, 1).ToString();
                ZW = System.Text.Encoding.Default.GetBytes(CharStr);
                // 得到汉字符的字节数组               
                if (ZW.Length == 2)
                {
                    int i1 = (short)(ZW[0]);
                    int i2 = (short)(ZW[1]);
                    ChineseStr_int = i1 * 256 + i2;
                    if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
                    {
                        ChinaStr = "A";
                    }
                    else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
                    {
                        ChinaStr = "B";
                    }
                    else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
                    {
                        ChinaStr = "C";
                    }
                    else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
                    {
                        ChinaStr = "D";
                    }
                    else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
                    {
                        ChinaStr = "E";
                    }
                    else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
                    {
                        ChinaStr = "F";
                    }
                    else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
                    {
                        ChinaStr = "G";
                    }
                    else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
                    {
                        ChinaStr = "H";
                    }
                    else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
                    {
                        ChinaStr = "J";
                    }
                    else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
                    {
                        ChinaStr = "K";
                    }
                    else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
                    {
                        ChinaStr = "L";
                    }
                    else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
                    {
                        ChinaStr = "M";
                    }
                    else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
                    {
                        ChinaStr = "N";
                    }
                    else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
                    {
                        ChinaStr = "O";
                    }
                    else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
                    {
                        ChinaStr = "P";
                    }
                    else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
                    {
                        ChinaStr = "Q";
                    }
                    else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
                    {
                        ChinaStr = "R";
                    }
                    else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
                    {
                        ChinaStr = "S";
                    }
                    else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
                    {
                        ChinaStr = "T";
                    }
                    else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
                    {
                        ChinaStr = "W";
                    }
                    else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
                    {
                        ChinaStr = "X";
                    }
                    else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
                    {
                        ChinaStr = "Y";
                    }
                    else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
                    {
                        ChinaStr = "Z";
                    }
                }
                else
                {
                    Capstr = Capstr + CharStr;
                }
                Capstr = Capstr + ChinaStr;
            }
            return Capstr;
        }

        #endregion

        #region 返回学校下拉菜单加载
        /// <summary>
        /// 返回学校下拉菜单加载JSON
        /// </summary>
        /// <returns>加载JSON</returns>
        public static string GetSclJson()
        {
            string sclJsonList = "";
            sclJsonList += "[";
            sclJsonList += "{id:'1',name:'汉南小学'},";
            sclJsonList += "{id:'2',name:'银莲湖小学'},";
            sclJsonList += "{id:'3',name:'水洪小学'},";
            sclJsonList += "{id:'4',name:'邓南中心小学'},";
            sclJsonList += "{id:'5',name:'江下小学'},";
            sclJsonList += "{id:'6',name:'大嘴小学'},";
            sclJsonList += "{id:'7',name:'乌金学校'},";
            sclJsonList += "{id:'8',name:'东城院中心小学'},";
            sclJsonList += "{id:'9',name:'育才小学'},";
            sclJsonList += "{id:'10',name:'纱帽山小学'},";
            sclJsonList += "{id:'11',name:'子林小学'}";
            sclJsonList += "]";
            return sclJsonList;
        }
        #endregion

    }
}
